package com.timesplus.generator.service.impl;


import com.timesplus.generator.dao.SysGeneratorDao;
import com.timesplus.generator.service.SysGeneratorService;
import com.timesplus.generator.utils.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 *  
 *  
 */
@Service
public class SysGeneratorServiceImpl implements SysGeneratorService {

	@Autowired
	private SysGeneratorDao sysGeneratorDao;

	@Override
	public List<Map<String, Object>> queryList( Map<String, Object> map ) {
		return sysGeneratorDao.queryList( map );
	}

	@Override
	public int queryTotal( Map<String, Object> map ) {
		return sysGeneratorDao.queryTotal( map );
	}

	@Override
	public Map<String, String> queryTable( String tableName ) {
		return sysGeneratorDao.queryTable( tableName );
	}

	@Override
	public List<Map<String, String>> queryColumns( String tableName ) {
		return sysGeneratorDao.queryColumns( tableName );
	}

	@Override
	public byte[] generatorCode( String[] tableNames ) throws Exception {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream( outputStream );

		for( String tableName : tableNames ) {
			// 查询表信息
			Map<String, String> table = queryTable( tableName );
			// 查询列信息
			List<Map<String, String>> columns = queryColumns( tableName );
			// 生成代码
			GenUtils.generatorCode( table, columns, zip );
		}
		IOUtils.closeQuietly( zip );
		return outputStream.toByteArray();
	}

}
